<template>
  <!-- 文章评论组件 -->
  <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad" :error.sync="error"
    error-text="请求失败，点击重新加载">
    <!-- <van-cell v-for="item in list" :key="item.com_id + ''" :title="item.content" /> -->
    <commentItem @clickReply="$emit('clickReply',$event)" v-for="item in list" :key="item.com_id + ''" :comment="item" @liking="item.is_liking = $event" />
  </van-list>
</template>

<script>
import { getCommentList } from '@/api/comment.js'
import commentItem from '@/components/article-item/comment-item.vue'
export default {
  data () {
    return {
      loading: false,
      finished: false,
      // 每页数据
      limit: 10,
      // 第几页开始
      offset: null,
      // 评论条数
      totalCount: 0,
      // 失败提示
      error: false
    }
  },
  props: {
    // 评论类型
    type: {
      type: String,
      default: 'a'
    },
    // 文章/评论id
    source: {
      type: [Number, String],
      require: true
    },
    // 评论列表数据
    list: {
      type: Array,
      require: true
    }
  },
  methods: {
    async onLoad () {
      try {
        const { data: res } = await getCommentList({
          offset: this.offset,
          limit: this.limit,
          type: this.type,
          source: this.source
        })
        this.list.push(...res.data.results)
        this.totalCount = res.data.total_count
        this.$emit('updataTotal', this.totalCount)
        if (this.list.length === this.totalCount) {
          this.finished = true
        } else {
          this.offset = res.data.last_id
        }
      } catch (err) {
        this.error = true
      }
      this.loading = false
    }
  },
  computed: {},
  watch: {},
  filters: {},
  components: {
    commentItem
  },
  created () {
    // console.log(this.type)
  },
  mounted () { }
}
</script>

<style scoped lang='less'>
</style>
